package ac.svn.accesscontrol.ui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.border.EtchedBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import ac.svn.accesscontrol.store.SVNTag;

public class StyledTextPane extends JTextPane
{
	public static final String STYLE_DEFAULT 				= "SDefault";
	public static final String STYLE_TAG_VALUE  			= "STValue";
	public static final String STYLE_TAG_DETAILS  		= "STDetails";
	public static final String STYLE_HEADER  				= "SHeader";
	public static final String STYLE_TAG_COMMENT 	= "STComment";
	public static final String STYLE_LINE_NUMBER		= "SLineNumber"; 
	public static final String STYLE_TAG_ERROR			= "Error";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private StyledDocument styleDoc;
	public StyledTextPane()
	{
		super();
		styleDoc = getStyledDocument();
		updateStyle();
	}

	private void updateStyle()
	{
		Style defStyle = styleDoc.getStyle(StyleContext.DEFAULT_STYLE);

		Style style = styleDoc.addStyle(STYLE_LINE_NUMBER, defStyle);
		StyleConstants.setBold(style, true);
		StyleConstants.setFontSize(style, 12);

		style = styleDoc.addStyle(STYLE_DEFAULT, defStyle);

		style  = styleDoc.addStyle(STYLE_TAG_COMMENT, defStyle);
		StyleConstants.setForeground(style, Color.GREEN);
		StyleConstants.setFontSize(style, 12);
		StyleConstants.setItalic(style, true);

		style  = styleDoc.addStyle(STYLE_TAG_VALUE, defStyle);
		StyleConstants.setForeground(style, Color.BLUE);
		StyleConstants.setFontSize(style, 14);
		StyleConstants.setBold(style, true);	

		style  = styleDoc.addStyle(STYLE_TAG_ERROR, defStyle);
		StyleConstants.setForeground(style, Color.RED);
		StyleConstants.setBold(style, true);
		
		style  = styleDoc.addStyle(STYLE_TAG_DETAILS, defStyle);
		StyleConstants.setForeground(style, Color.DARK_GRAY);
		StyleConstants.setFontSize(style, 13);
		
		style  = styleDoc.addStyle(STYLE_HEADER, defStyle);
		StyleConstants.setForeground(style, Color.BLACK);
		StyleConstants.setBackground(style, Color.LIGHT_GRAY);
		StyleConstants.setFontFamily(style, "Tahoma");
		StyleConstants.setFontSize(style, 14);
		StyleConstants.setBold(style, true);
		StyleConstants.setUnderline(style, true);
	}

	public void showTag(SVNTag svnTag)
	{
		setText("");
		removeAll();
		if(null != svnTag)
		{
			insertHeader("  Line:  ");
			insertText("\n", STYLE_DEFAULT);
			insertText(String.format("%6d\n", svnTag.getLineNumber()),STYLE_LINE_NUMBER);

			if(null != svnTag.getTagHeader())
			{
				insertText("\n", STYLE_DEFAULT);
				insertHeader("  Comments:  ");
				insertText("\n", STYLE_DEFAULT);
				insertText(String.format("%s\n",svnTag.getTagHeader().replaceAll("#","#")), STYLE_TAG_COMMENT);
			}

			insertText("\n", STYLE_DEFAULT);
			insertHeader("  Tag Value:  ");
			insertText("\n", STYLE_DEFAULT);
			insertText(String.format("%s\n",svnTag.getTagText()), STYLE_TAG_VALUE);

			insertText("\n", STYLE_DEFAULT);
			insertHeader("  Details:  ");
			insertText("\n", STYLE_DEFAULT);
			for(String line: svnTag.getLines())
			{
				insertText(String.format("%s\n", line), STYLE_TAG_DETAILS);
			}
		}
		else
		{
			insertText("Error: Tag is not correct!", STYLE_TAG_ERROR);
		}
	}

	private void insertText(String value,String styleName)
	{
		try 
		{
			styleDoc.insertString(styleDoc.getLength(), value, styleDoc.getStyle(styleName));
			setCaretPosition(styleDoc.getLength());
		}
		catch (BadLocationException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void insertHeader(String header)
	{
		JLabel label = new JLabel(header,JLabel.CENTER);
		label.setOpaque(true);
		label.setBackground(new Color(104,204,255));
		label.setBorder(new EtchedBorder(EtchedBorder.RAISED));
		label.setFont(new Font("Tahoma", Font.BOLD, 14));
		insertComponent(label);
	}
}
